/*
 * @Author: yishen@mdashen.com
 * @Date: 2021-05-29 16:10:51
 * @LastEditTime: 2021-05-29 16:17:24
 * @Description: 点击外部
 */
import { ref, onMounted, onUnmounted, Ref } from 'vue'
const useClickOutsite = (elementRef: Ref<null | HTMLElement>) => {
  const isClickOutsite = ref(false)
  const hander = (e: MouseEvent) => {
    if (elementRef.value) {
      if (elementRef.value.contains(e.target as HTMLElement)) {
        isClickOutsite.value = false
      } else {
        isClickOutsite.value = true
      }
    }
  }
  onMounted(() => {
    document.addEventListener('click', hander)
  })
  onUnmounted(() => {
    document.removeEventListener('click', hander)
  })
  return isClickOutsite
}

export default useClickOutsite
